<template>
  <div>
    <!-- 下载压缩文件 -->
  </div>
</template>
<script>
import JSZip from 'jszip'
export default {
  data() {
    return {}
  },
  mounted() {
    this.$bus.$on('downloadZip', (v) => {
      let { level } = this.$store.getters
      if (level != 1) {
        this.warnMsg('抱歉，您没有下载权限')
        return
      }
      if (!v || v.length == 0) return
      this.downloadByZip(v)
    })
  },
  beforeDestroy() {
    this.$bus.$off('downloadZip')
  },
  methods: {
    async downloadByZip(fileList) {
      const zip = new JSZip()
      for (let i = 0; i < fileList.length; i++) {
        const blob = await this.$store.dispatch(
          'downloadFileTbNetDisk',
          fileList[i].id,
        )
        zip.file(fileList[i].oldName, blob, { base64: true })
      }
      zip.generateAsync({ type: 'blob' }).then((content) => {
        location.href = URL.createObjectURL(content)
      })
    },
    getImgBlob(url) {
      return new Promise((resolve) => {
        const xhr = new XMLHttpRequest()
        xhr.open('get', url, true)
        xhr.responseType = 'blob'
        xhr.onload = () => {
          resolve(xhr.response)
        }
        xhr.send()
      })
    },
  },
}
</script>
<style scoped lang="scss">
</style>